package cn.syc.struck.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Arrays;
import java.util.List;

import cn.syc.struck.demo.base.BaseActivity;

public class SettingHostActivity extends BaseActivity {

    // Content View Elements

    private LinearLayout linearLayout;
    private TextView textView_serveradd;
    private RadioGroup radioGroup_cc;
    private RadioButton radioButton_url1;
    private RadioButton radioButton_url2;
    private RadioButton radioButton_url3;
    private EditText editText_api_url;
    private Button button_ok;

    // End Of Content View Elements

    private void bindViews() {

        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        textView_serveradd = (TextView) findViewById(R.id.textView_serveradd);
        radioGroup_cc = (RadioGroup) findViewById(R.id.radioGroup_cc);
        radioButton_url1 = (RadioButton) findViewById(R.id.radioButton_url1);
        radioButton_url2 = (RadioButton) findViewById(R.id.radioButton_url2);
        radioButton_url3 = (RadioButton) findViewById(R.id.radioButton_url3);
        editText_api_url = (EditText) findViewById(R.id.editText_api_url);
        button_ok = (Button) findViewById(R.id.button_ok);
    }

    List<String> __urls = Arrays.asList("https://www.struck.cn/", "http://192.168.0.11/", "http://192.168.0.21:8080",
            "http://192.168.0.199:8080");

    String[] urls = {"http://10.0.2.2:8080", "http://192.168.43.1:8080", "http://192.168.0.100:8080"};

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting_host;
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        //-------------
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        bindViews();
        editText_api_url.setText(App.getInstance().getPrefValue(__horst_url, "http://10.0.2.2:8080"));
        initListener();
    }

    private String __horst_url = "setting.horst_url";

    private void initListener() {
        radioGroup_cc.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if (checkedId == radioButton_url1.getId()) {
                    editText_api_url.setText(urls[0]);
                }
                if (checkedId == radioButton_url2.getId()) {
                    editText_api_url.setText(urls[1]);
                }
                if (checkedId == radioButton_url3.getId()) {
                    editText_api_url.setText(urls[2]);
                }
            }
        });

        button_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url_host = editText_api_url.getText().toString().trim();


                if (TextUtils.isEmpty(url_host)) {
                    Toast.makeText(SettingHostActivity.this, "url is empty!", Toast.LENGTH_SHORT).show();
                } else {
                    //StruckConfig.setUrlHost(url_host);
                    App.getInstance().setPrefValue(__horst_url, url_host);
                    Toast.makeText(SettingHostActivity.this, "已保存!", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    //-------------------------
}
